package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.module;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.vo.scene.AdditionalProductHeadModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.AdditionalProductModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.GroupProdAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.OfferInstModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.OfferInstVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdAttrModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdAttrVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProdRoleModuleVO;
import com.iwhalecloud.bss.kite.client.vo.scene.ProductModuleVO;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.CuccOfferInstVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.OfferRelModuleVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneNewOfferSeneVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.SceneProductModuleVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.web.service.accept.bo.AcceptContext;
import com.iwhalecloud.bss.kite.web.service.accept.bo.SceneModule;
import com.iwhalecloud.bss.kite.web.service.accept.handler.module.AbstractModuleHandler;

import com.ztesoft.bss.common.util.SpringUtil;

/**
 * 追加配件
 *
 * @author zou.qinghua
 */
@Service
public class AddPartsInfoModuleHandler extends AbstractModuleHandler {

    @Override
    public List<? extends ModuleVO> execute(AcceptContext context, SceneModule sceneModule) {
        List result = new ArrayList<>();
        String serviceOfferId = context.getAcceptRequest().getServiceOfferId();
        SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
        if (KiteStringUtils.isEqual(LocalServiceOfferConsts.SERVICE_OFFER_ADD_PARTS, serviceOfferId)) {
            SceneProductModuleVO sceneProductModuleVO = sceneNewOfferSeneVO.getSceneProductModuleVO();
            OfferRelModuleVO offerRelModuleVO = sceneProductModuleVO.getOfferRelModuleVO();
            offerRelModuleVO.setModuleName("追加信息");
            OfferInstModuleVO offerInstModuleVO = sceneProductModuleVO.getOffer();
            List<OfferInstVO> offerInstVOS = offerInstModuleVO.getOfferInstList();
            if (!KiteListUtils.isEmpty(offerInstVOS)) {
                offerInstVOS.stream().forEach(offerInstVO -> {
                    CuccOfferInstVO cuccOfferInstVO = (CuccOfferInstVO) offerInstVO;
                    List<ProductModuleVO> productModuleVOS = cuccOfferInstVO.getProduct();
                    if (Objects.nonNull(productModuleVOS)) {
                        productModuleVOS.stream().forEach(productModuleVO -> {
                            List<ProdRoleModuleVO> prodRoleModuleVOS = productModuleVO.getProdRole();
                            prodRoleModuleVOS.stream().forEach(prodRoleModuleVO -> {
                                List<GroupProdAttrModuleVO> groupProdAttrModuleVOS = prodRoleModuleVO.getGroupProdAttr();
                                groupProdAttrModuleVOS.stream().forEach(groupProdAttrModuleVO -> {
                                    List<ProdAttrVO> prodAttrVOS = groupProdAttrModuleVO.getFormProdAttrList();
                                    if (KiteListUtils.isNotEmpty(prodAttrVOS)) {
                                        prodAttrVOS.stream().forEach(prodAttrVO ->
                                            prodAttrVO.setIsEdit("F")
                                        );
                                    }
                                    groupProdAttrModuleVO.setButtonAttrList(new ArrayList<>());
                                    groupProdAttrModuleVO.setIsEdit("F");
                                    groupProdAttrModuleVO.setShowDelete("F");
                                    groupProdAttrModuleVO.setShowEdit("F");
                                    groupProdAttrModuleVO.setShowPreview("T");
                                });
                            });
                        });
                    }

                });
            }
            List<ProductModuleVO> productModuleVOS = sceneProductModuleVO.getProduct();
            if (!KiteListUtils.isEmpty(productModuleVOS)) {
                productModuleVOS.stream().forEach(productModuleVO -> {
                    List<ProdRoleModuleVO> prodRoleModuleVOS = productModuleVO.getProdRole();
                    prodRoleModuleVOS.stream().forEach(prodRoleModuleVO -> {
                        List<GroupProdAttrModuleVO> groupProdAttrModuleVOS = prodRoleModuleVO.getGroupProdAttr();
                        groupProdAttrModuleVOS.stream().forEach(groupProdAttrModuleVO -> {
                            List<ProdAttrVO> prodAttrVOS = groupProdAttrModuleVO.getFormProdAttrList();
                            if (KiteListUtils.isNotEmpty(prodAttrVOS)) {
                                prodAttrVOS.stream().forEach(prodAttrVO ->
                                    prodAttrVO.setIsEdit("F")
                                );
                            }
                            groupProdAttrModuleVO.setButtonAttrList(new ArrayList<>());
                            groupProdAttrModuleVO.setIsEdit("F");
                            groupProdAttrModuleVO.setShowDelete("F");
                            groupProdAttrModuleVO.setShowEdit("F");
                            groupProdAttrModuleVO.setShowPreview("T");
                        });
                    });

                    AdditionalProductHeadModuleVO additionalProductHeadModuleVO = productModuleVO.getAddProdModule();
                    if (!KiteObjectUtils.isEmpty(additionalProductHeadModuleVO)) {
                        List<ModuleVO> moduleVOS = additionalProductHeadModuleVO.getModules();
                        if (KiteListUtils.isNotEmpty(moduleVOS)) {
                            List<String> ids = new ArrayList<>();
                            for (int i = 1; i < moduleVOS.size(); i++) {
                                AdditionalProductModuleVO moduleVO = (AdditionalProductModuleVO) moduleVOS.get(i);
                                List<ProdRoleModuleVO> roleModuleVOS = moduleVO.getList();
                                for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                    String actionType = prodRoleModuleVO.getActionType();
                                    ProdAttrModuleVO prodAttrModuleVO = prodRoleModuleVO.getProdAttr();
                                    List<ProdAttrVO> prodAttrVOS = prodAttrModuleVO.getProdAttrList();
                                    boolean hasAttr = false;
                                    for (ProdAttrVO prodAttrVO : prodAttrVOS) {
                                        String fieldName = prodAttrVO.getFieldName();
                                        if (KiteStringUtils.isEqual("210004184", fieldName)) {
                                            hasAttr = true;
                                        }
                                    }
                                    if (!hasAttr) {
                                        ids.add(moduleVO.getOfferId());
                                    }
                                }
                            }
                            CommonAttrModuleVO moduleVO = (CommonAttrModuleVO) moduleVOS.get(0);
                            List<CommonAttrVO> commonAttrVOS = moduleVO.getCommonAttrList();
                            commonAttrVOS.stream().forEach(commonAttrVO -> {
                                String id = commonAttrVO.getFieldName();
                                if (ids.contains(id)) {
                                    commonAttrVO.setIsEdit("F");
                                    commonAttrVO.setShowType("H");
                                }
                                else {
                                    commonAttrVO.setIsEdit("T");
                                    commonAttrVO.setShowType("D");
                                }
                            });
                            for (int i = 1; i < moduleVOS.size(); i++) {
                                AdditionalProductModuleVO moduleVO1 = (AdditionalProductModuleVO) moduleVOS.get(i);
                                String id = moduleVO1.getOfferId();
                                if (ids.contains(id)) {
                                    moduleVO1.setIsEdit("F");
                                    moduleVO1.setIsShow("F");
                                    List<ProdRoleModuleVO> roleModuleVOS = moduleVO1.getList();
                                    if (KiteListUtils.isNotEmpty(roleModuleVOS)) {
                                        for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                            ProdAttrModuleVO prodAttr = prodRoleModuleVO.getProdAttr();
                                            List<ProdAttrVO> prodAttrVOS = prodAttr.getProdAttrList();
                                            prodAttrVOS.stream().forEach(prodAttrVO ->
                                                prodAttrVO.setIsEdit("F")
                                            );
                                        }
                                    }
                                }
                                else {
                                    moduleVO1.setIsEdit("T");
                                    moduleVO1.setIsShow("T");
                                    List<ProdRoleModuleVO> roleModuleVOS = moduleVO1.getList();
                                    if (KiteListUtils.isNotEmpty(roleModuleVOS)) {
                                        for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                            ProdAttrModuleVO prodAttr = prodRoleModuleVO.getProdAttr();
                                            List<ProdAttrVO> prodAttrVOS = prodAttr.getProdAttrList();
                                            prodAttrVOS.stream().forEach(prodAttrVO ->
                                                prodAttrVO.setIsEdit("T")
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }
        return result;
    }

    @Override
    public List<? extends ModuleVO> previewSceneVo(AcceptContext context, SceneModule sceneModule) {
        List result = new ArrayList<>();
        String sceneInstId = context.getAcceptRequest().getSceneInstId();
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(sceneInstId);
        String serviceOfferId = kiteSceneInst.getServiceOfferId();
        if (KiteStringUtils.isEqual(LocalServiceOfferConsts.SERVICE_OFFER_ADD_PARTS, serviceOfferId)) {
            SceneNewOfferSeneVO sceneNewOfferSeneVO = (SceneNewOfferSeneVO) context.getSceneVo();
            SceneProductModuleVO sceneProductModuleVO = sceneNewOfferSeneVO.getSceneProductModuleVO();
            OfferRelModuleVO offerRelModuleVO = sceneProductModuleVO.getOfferRelModuleVO();
            offerRelModuleVO.setModuleName("追加信息");
            OfferInstModuleVO offerInstModuleVO = sceneProductModuleVO.getOffer();
            List<OfferInstVO> offerInstVOS = offerInstModuleVO.getOfferInstList();
            if (!KiteListUtils.isEmpty(offerInstVOS)) {
                offerInstVOS.stream().forEach(offerInstVO -> {
                    CuccOfferInstVO cuccOfferInstVO = (CuccOfferInstVO) offerInstVO;
                    List<ProductModuleVO> productModuleVOS = cuccOfferInstVO.getProduct();
                    if (Objects.nonNull(productModuleVOS)) {
                        productModuleVOS.stream().forEach(productModuleVO -> {
                            List<ProdRoleModuleVO> prodRoleModuleVOS = productModuleVO.getProdRole();
                            prodRoleModuleVOS.stream().forEach(prodRoleModuleVO -> {
                                List<GroupProdAttrModuleVO> groupProdAttrModuleVOS = prodRoleModuleVO
                                    .getGroupProdAttr();
                                groupProdAttrModuleVOS.stream().forEach(groupProdAttrModuleVO -> {
                                    List<ProdAttrVO> prodAttrVOS = groupProdAttrModuleVO.getFormProdAttrList();
                                    if (KiteListUtils.isNotEmpty(prodAttrVOS)) {
                                        prodAttrVOS.stream().forEach(prodAttrVO -> prodAttrVO.setIsEdit("F"));
                                    }
                                    groupProdAttrModuleVO.setButtonAttrList(new ArrayList<>());
                                    groupProdAttrModuleVO.setIsEdit("F");
                                    groupProdAttrModuleVO.setShowDelete("F");
                                    groupProdAttrModuleVO.setShowEdit("F");
                                    groupProdAttrModuleVO.setShowPreview("T");
                                });
                            });
                        });
                    }
                });
            }
            List<ProductModuleVO> productModuleVOS = sceneProductModuleVO.getProduct();
            if (!KiteListUtils.isEmpty(productModuleVOS)) {
                productModuleVOS.stream().forEach(productModuleVO -> {
                    List<ProdRoleModuleVO> prodRoleModuleVOS = productModuleVO.getProdRole();
                    prodRoleModuleVOS.stream().forEach(prodRoleModuleVO -> {
                        List<GroupProdAttrModuleVO> groupProdAttrModuleVOS = prodRoleModuleVO.getGroupProdAttr();
                        groupProdAttrModuleVOS.stream().forEach(groupProdAttrModuleVO -> {
                            List<ProdAttrVO> prodAttrVOS = groupProdAttrModuleVO.getFormProdAttrList();
                            if (KiteListUtils.isNotEmpty(prodAttrVOS)) {
                                prodAttrVOS.stream().forEach(prodAttrVO ->
                                    prodAttrVO.setIsEdit("F")
                                );
                            }
                            groupProdAttrModuleVO.setButtonAttrList(new ArrayList<>());
                            groupProdAttrModuleVO.setIsEdit("F");
                            groupProdAttrModuleVO.setShowDelete("F");
                            groupProdAttrModuleVO.setShowEdit("F");
                            groupProdAttrModuleVO.setShowPreview("T");
                        });
                    });

                    AdditionalProductHeadModuleVO additionalProductHeadModuleVO = productModuleVO.getAddProdModule();
                    if (!KiteObjectUtils.isEmpty(additionalProductHeadModuleVO)) {
                        List<ModuleVO> moduleVOS = additionalProductHeadModuleVO.getModules();
                        if (KiteListUtils.isNotEmpty(moduleVOS)) {
                            List<String> ids = new ArrayList<>();
                            for (int i = 1; i < moduleVOS.size(); i++) {
                                AdditionalProductModuleVO moduleVO = (AdditionalProductModuleVO) moduleVOS.get(i);
                                List<ProdRoleModuleVO> roleModuleVOS = moduleVO.getList();
                                for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                    String actionType = prodRoleModuleVO.getActionType();
                                    ProdAttrModuleVO prodAttrModuleVO = prodRoleModuleVO.getProdAttr();
                                    List<ProdAttrVO> prodAttrVOS = prodAttrModuleVO.getProdAttrList();
                                    boolean hasAttr = false;
                                    for (ProdAttrVO prodAttrVO : prodAttrVOS) {
                                        String fieldName = prodAttrVO.getFieldName();
                                        if (KiteStringUtils.isEqual("210004184", fieldName)) {
                                            hasAttr = true;
                                        }
                                    }
                                    if (!hasAttr) {
                                        ids.add(moduleVO.getOfferId());
                                    }
                                }
                            }
                            CommonAttrModuleVO moduleVO = (CommonAttrModuleVO) moduleVOS.get(0);
                            List<CommonAttrVO> commonAttrVOS = moduleVO.getCommonAttrList();
                            commonAttrVOS.stream().forEach(commonAttrVO -> {
                                String id = commonAttrVO.getFieldName();
                                if (ids.contains(id)) {
                                    commonAttrVO.setIsEdit("F");
                                    commonAttrVO.setShowType("H");
                                }
                                else {
                                    commonAttrVO.setIsEdit("T");
                                    commonAttrVO.setShowType("D");
                                }
                            });
                            for (int i = 1; i < moduleVOS.size(); i++) {
                                AdditionalProductModuleVO moduleVO1 = (AdditionalProductModuleVO) moduleVOS.get(i);
                                String id = moduleVO1.getOfferId();
                                if (ids.contains(id)) {
                                    moduleVO1.setIsEdit("F");
                                    moduleVO1.setIsShow("F");
                                    List<ProdRoleModuleVO> roleModuleVOS = moduleVO1.getList();
                                    if (KiteListUtils.isNotEmpty(roleModuleVOS)) {
                                        for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                            ProdAttrModuleVO prodAttr = prodRoleModuleVO.getProdAttr();
                                            List<ProdAttrVO> prodAttrVOS = prodAttr.getProdAttrList();
                                            prodAttrVOS.stream().forEach(prodAttrVO ->
                                                prodAttrVO.setIsEdit("F")
                                            );
                                        }
                                    }
                                }
                                else {
                                    moduleVO1.setIsEdit("T");
                                    moduleVO1.setIsShow("T");
                                    List<ProdRoleModuleVO> roleModuleVOS = moduleVO1.getList();
                                    if (KiteListUtils.isNotEmpty(roleModuleVOS)) {
                                        for (ProdRoleModuleVO prodRoleModuleVO : roleModuleVOS) {
                                            ProdAttrModuleVO prodAttr = prodRoleModuleVO.getProdAttr();
                                            List<ProdAttrVO> prodAttrVOS = prodAttr.getProdAttrList();
                                            prodAttrVOS.stream().forEach(prodAttrVO ->
                                                prodAttrVO.setIsEdit("T")
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }
        return result;
    }
}
